#include <rtthread.h>

#define LOG_LVL     LOG_LVL_DBG
#define LOG_TAG     "pms"
#include <ulog.h>

static rt_timer_t pms_log_timer = RT_NULL;
static rt_bool_t pms_log_timer_flag = RT_FALSE;

#define PMS_LOG_DUMP_TIMEOUT       60*1000

struct pms_log_info
{
    uint16_t power; // mAh
    uint16_t voltage; //vbat
    uint16_t display_time; //lcd on time
    uint32_t deepsleep_ticks; // in deepsleep mode ticks
    uint32_t wakeup_cnt; //wakeup count
};

struct pms_log_info g_pms_log_info = { 0 };

void pms_log_dump(void *param)
{
    g_pms_log_info.deepsleep_ticks = 15 * 1000;
    g_pms_log_info.power = 100;
    g_pms_log_info.voltage = 3800;
    g_pms_log_info.display_time += 5;

    LOG_D(" %d,%d,%d,%d,%d", g_pms_log_info.power,
        g_pms_log_info.voltage,
        g_pms_log_info.deepsleep_ticks,
        g_pms_log_info.wakeup_cnt,
        g_pms_log_info.display_time);
}

int pms_log_timer_init(void)
{
    if (pms_log_timer == RT_NULL)
    {
        pms_log_timer = rt_timer_create("pm_log", pms_log_dump, RT_NULL,
            PMS_LOG_DUMP_TIMEOUT,
            RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
        pms_log_timer_flag = RT_FALSE;
    }

    return RT_EOK;
}

int pms_log_timer_start(void)
{
    if (pms_log_timer_flag == RT_FALSE)
    {
        if (pms_log_timer != RT_NULL)
        {
            rt_timer_start(pms_log_timer);
            pms_log_timer_flag = RT_TRUE;
        }
    }

    return RT_EOK;
}

int pms_log_timer_stop(void)
{
    if (pms_log_timer_flag == RT_TRUE)
    {
        if (pms_log_timer != RT_NULL)
        {
            rt_timer_stop(pms_log_timer);
            pms_log_timer_flag = RT_FALSE;
        }
    }

    return RT_EOK;
}

MSH_CMD_EXPORT(pms_log_timer_start, pms log timer start);
MSH_CMD_EXPORT(pms_log_timer_stop, pms log timer stop);
